package es.guiguegon.sineview.sample.slice;

import es.guiguegon.sineview.SineView;
import es.guiguegon.sineview.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    SineView sineView1;

    SineView sineView2;

    SineView sineView3;

    SineView sineView4;
    Image playButton;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        sineView1 = (SineView) findComponentById(ResourceTable.Id_sine_view_1);
        sineView2 = (SineView) findComponentById(ResourceTable.Id_sine_view_2);
        sineView3 = (SineView) findComponentById(ResourceTable.Id_sine_view_3);
        sineView4 = (SineView) findComponentById(ResourceTable.Id_sine_view_4);
        playButton = (Image) findComponentById(ResourceTable.Id_img_play);

        sineView1.setClickedListener(this::onClick);
        sineView2.setClickedListener(this::onClick);
        sineView3.setClickedListener(this::onClick);
        sineView4.setClickedListener(this::onClick);
        playButton.setClickedListener(this::onClick);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {

        switch (component.getId()) {
            case ResourceTable.Id_sine_view_1:
                if (sineView1.isAnimating()) {
                    sineView1.pauseWave();
                } else {
                    sineView1.resumeWave();
                }
                break;
            case ResourceTable.Id_sine_view_2:
                if (sineView2.isAnimating()) {
                    sineView2.pauseWave();
                } else {
                    sineView2.resumeWave();
                }
                break;
            case ResourceTable.Id_sine_view_3:
                if (sineView3.isAnimating()) {
                    sineView3.pauseWave();
                } else {
                    sineView3.resumeWave();
                }
                break;
            case ResourceTable.Id_sine_view_4:
                if (sineView4.isAnimating()) {
                    sineView4.pauseWave();
                } else {
                    sineView4.resumeWave();
                }
                break;
            case ResourceTable.Id_img_play:
                if (sineView1.isAnimating()) {
                    sineView1.stopWave();
                    sineView2.stopWave();
                    sineView3.stopWave();
                    sineView4.stopWave();
                    playButton.setPixelMap(ResourceTable.Media_icon_play);
                } else {
                     sineView1.startWave();
                    sineView2.startWave();
                    sineView3.startWave();
                    sineView4.startWave();
                    playButton.setPixelMap(ResourceTable.Media_icon_pause);
                }
                break;
            default:
        }
    }
}
